package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Item;
import cn.wolfcode.trip.base.mapper.ItemMapper;
import cn.wolfcode.trip.base.query.ItemQueryObject;
import cn.wolfcode.trip.base.service.IItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements IItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageInfo query(ItemQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Item> list = itemMapper.queryForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void delete(Long id) {
        itemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Item> selectAll() {
        return itemMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Item item) {
        if(item.getId() == null){
            item.setInputTime(new Date());
            item.setCount(0);
            itemMapper.insert(item);
        }else {
            itemMapper.updateByPrimaryKey(item);
        }
    }

    @Override
    public List<Item> getItemsByCatalogId(Long catalogId) {
        return itemMapper.selectItemsByCatalogId(catalogId);
    }

    /**
     * 获取商品的价格
     * @param itemId 商品id
     * @return
     */
    @Override
    public int getItemPriceByItemId(Long itemId) {
        return itemMapper.selectItemPriceByItemId(itemId);
    }

    @Override
    public Item get(Long id) {
        return itemMapper.selectByPrimaryKey(id);
    }

}
